<?php

namespace App\Http\Controllers;

use App\Notifications\RegisterMailNotify;
use App\User;
use Illuminate\Http\Request;

class ResendEmailController extends Controller
{
	//显示邮件发送模板
	public function send(){
		//判断用户是否登录，如果没有登录则不让返回首页
		if(\Auth::check ()){
			$id=Auth()->user ()->id;
			$user=User::find($id);
			if($user['status']){
				session()->flash('success','激活成功！欢迎回来！');
				return redirect ('/home');
			}
			return view('send');
		}else{
			return redirect ('/login');
		}

	}
	//重新发送邮件
	public function resend(){
		//判断用户是否登录，如果登录才执行发送邮件的程序，否则直接返回首页
		if(\Auth::check ()){
			$user=\Auth::user ();
			\Notification::send ($user,new RegisterMailNotify($user));
			return redirect ()->back();
		}else{
			return redirect ('/login');
		}
	}
	//账号激活
	public function active($email_token){
		//找到数据中对应的用户
		$user=User::where('email_token',$email_token)->first();
		if($user){
			//激活用户
			$user->status=1;
			$user->save();
			session()->flash('success','激活成功！欢迎回来！');
			return redirect ('/home');
		}else{
			//如果没有找到，则不是通过邮件点击跳转的页面
			session()->flash('danger','账号非法！');
			return redirect ('/');
		}
	}
}
